<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 物流接口
 * @ApiInternal
 */
class Express extends Api
{
    protected $noNeedLogin = ['company'];
    protected $noNeedRight = '*';

    /**
     * 获取物流公司列表
     *
     * @ApiTitle    (物流公司列表)
     * @ApiSummary  (获取支持查询的物流公司列表)
     * @ApiMethod   (GET)
     * @ApiReturn   ({
     *  "code": 1,
     *  "msg": "success",
     *  "data": [{
     *    "name": "顺丰速运",
     *    "code": "SF"
     *  }]
     * })
     */
    public function company()
    {
        // 获取阿里云配置
        $express_set = \app\admin\model\expressquery\ExpressqueryConfig::all();
        $express_set = $express_set[0];
        if (empty($express_set) || empty($express_set['aliappcode'])) {
            $this->error('配置参数错误');
        }

        $url = 'https://wdexpress.market.alicloudapi.com/globalExpressLists';
        $headers = [
            'Authorization:APPCODE 48d9d70b9744452787a8175d48fcd678'
        ];

        // 请求阿里云接口
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        
        $response = curl_exec($curl);
        
        if ($response === false) {
            $this->error('请求失败：' . curl_error($curl));
        }
        
        curl_close($curl);
    
        $result = json_decode($response, true);
        if (empty($result) || !isset($result['result'])) {
            $this->error('获取物流公司列表失败');
        }

        $this->success('', $result['result']);
    }
}